package com.avic.rong.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.adapter.vadio_adapter.RecyclerBaseAdapter;
import com.avic.rong.adapter.vadio_adapter.RecyclerItemBaseHolder;
import com.avic.rong.adapter.vadio_adapter.RecyclerItemViewHolder;
import com.avic.rong.adapter.vadio_adapter.RecyclerNormalAdapter;
import com.avic.rong.adapter.vadio_adapter.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TSJ-2018-0492 on 2019/9/24.
 */

public class FragmentVadio extends BaseFragment {

    private RecyclerView vadio_recycler;
    List<VideoModel> dataList = new ArrayList<>();
    RecyclerBaseAdapter recyclerBaseAdapter;
    @Override
    protected int getContentViewId() {
        return R.layout.vadio_fragment;
    }
    @Override
    protected void initView() {
        vadio_recycler = fragmentView.findViewById(R.id.vadio_recycler);
        LinearLayoutManager  linearLayoutManager = new LinearLayoutManager(getActivity());
        vadio_recycler.setLayoutManager(linearLayoutManager);
        resolveData();
        final RecyclerNormalAdapter recyclerNormalAdapter = new RecyclerNormalAdapter(getActivity(), dataList);
        vadio_recycler.setAdapter(recyclerNormalAdapter);
        vadio_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int firstVisibleItem, lastVisibleItem;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                //大于0说明有播放
                if (GSYVideoManager.instance().getPlayPosition() >= 0) {
                    //当前播放的位置
                    int position = GSYVideoManager.instance().getPlayPosition();
                    //对应的播放列表TAG
                    if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemViewHolder.TAG)
                            && (position < firstVisibleItem || position > lastVisibleItem)) {
                        //如果滑出去了上面和下面就是否，和今日头条一样
                        if(!GSYVideoManager.isFullState(getActivity())) {
                            GSYVideoManager.releaseAllVideos();
                            recyclerNormalAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

        });
    }

    @Override
    protected void addListener() {

    }

    public boolean onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(getActivity())) {
            return true;
        }
        return false;

    }


    @Override

    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }


    @Override
    public void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }



    private void resolveData() {
        for (int i = 0; i < 19; i++) {
            VideoModel videoModel = new VideoModel();
            dataList.add(videoModel);
        }

        if (recyclerBaseAdapter != null)
            recyclerBaseAdapter.notifyDataSetChanged();
    }

}
